<!-- 面包屑导航 -->
<template>
  <el-breadcrumb id="guide-breadcrumb" separator="/" class="breadcrumb">
    <transition-group name="breadcrumb">
      <el-breadcrumb-item
          v-for="(item, index) in breadcrumbData"
          :key="item.path"
        >
          <!-- 不可点击 -->
          <span v-if="index === breadcrumbData.length - 1" class="no-redirect">
            {{ generateTitle(item.meta.title) }}
          </span>
          <!-- 可以点击 -->
          <span v-else class="redirect" @click="onLinkClick(item)">
            {{ generateTitle(item.meta.title) }}
          </span>
      </el-breadcrumb-item>
      </transition-group>
    </el-breadcrumb>
  </template>

<script setup>
import { watch, ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useStore } from 'vuex'
import { generateTitle } from '@/utils/i18n'

const route = useRoute()
// 生成数组数据
const breadcrumbData = ref([])
const getBreadcrumbData = () => {
  breadcrumbData.value = route.matched.filter(
    item => item.meta && item.meta.title
  )
}

// 面包屑跳转
const router = useRouter()
const onLinkClick = item => {
  router.push(item.path)
}

// 绑定主题颜色
const store = useStore()
/* eslint-disable */
const linkHoverColor = ref(store.getters.cssVar.menuBg)

// 监听路由变化时触发
watch(
  route,
  () => {
    getBreadcrumbData()
  },
  {
    // 每次进入页面时都会执行
    immediate: true
  }
)
</script>
<style lang="scss" scoped>
.breadcrumb {
  display: inline-block;
  font-size: 14px;
  line-height: 50px;
  margin-left: 8px;
  ::v-deep .no-redirect {
    color: #97a8be;
    cursor: text;
  }

  .redirect {
    color: #666;
    font-weight: 600;
    cursor: pointer;
  }

  .redirect:hover {
    // 将来需要进行主题替换，所以这里不去写死样式
    color: v-bind(linkHoverColor);
  }
}
</style>
